import os
import tempfile
import shutil


class TempFileManager:
    """临时文件管理类，用于安全处理临时文件"""

    def __init__(self):
        self.temp_dir = tempfile.mkdtemp()

    def create_temp_file(self, suffix=".py", content=""):
        """创建临时文件"""
        with tempfile.NamedTemporaryFile(suffix=suffix, dir=self.temp_dir, delete=False, mode='w') as f:
            f.write(content)
            return f.name

    def cleanup(self):
        """清理临时文件"""
        try:
            shutil.rmtree(self.temp_dir)
        except Exception as e:
            print(f"清理临时文件时发生错误: {e}")
